package com.great.start;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
import org.jaudiotagger.audio.exceptions.ReadOnlyFileException;
import org.jaudiotagger.audio.mp3.MP3File;
import org.jaudiotagger.tag.TagException;

import com.great.common.dao.MusicDao;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;
import com.xiaoleilu.hutool.date.DateTime;
import com.xiaoleilu.hutool.date.DateUtil;

/**
 * 音乐播放器
 * @author Administrator
 *
 */
public class MusicAction {
	private  MusicDao musicdao = new MusicDao();
	
	/**
	 * 获取上次记忆顺序播放音乐
	 */
	public  void randomPlay(){
		
		List<Record> musicList = musicdao.getListMusic();
		
	}
	/**
	 * 更新歌单
	 */
	public void updateMusicList(){
		
		String localPath = "D:\\CloudMusic";
		List<String> MusicListName = Util.getDirectory(new File(localPath));
		List<Record> musicList = musicdao.getListMusic();
		 MP3File file;
		 String palytime = "";
		for (String localhostMusicName : MusicListName) {
			try {
				file = new MP3File(localhostMusicName);
                palytime=file.getMP3AudioHeader().getTrackLengthAsString();
			} catch (Exception e) {
				e.printStackTrace();
			}
			for (int i = 0; i < musicList.size(); i++) {
				Record music = musicList.get(i);
				String dbMusicName =  music.getStr("name");
				if(!localhostMusicName.equals(dbMusicName)){
					//存储
					musicdao.putMusic(localhostMusicName, palytime, "0", localPath, DateTime.now().toString(), "0");
				}
			}
		}
				
	}
	public static void main(String[] args) {
		MusicAction m = new MusicAction();
		m.updateMusicList();
	}
	public  void play(String musicPath) throws IOException{
		Runtime.getRuntime().exec("cmd /c start "   +   musicPath.replaceAll(" ", "\" \""));//直接使用系统播放器播放音乐
	}
	
	public void down(){
		
	}
}
